package com.music.mapper;

import com.music.pojo.Singer;
import com.music.pojo.SingerExample;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface SingerMapper {
    @SelectProvider(type=SingerSqlProvider.class, method="countByExample")
    long countByExample(SingerExample example);

    @DeleteProvider(type=SingerSqlProvider.class, method="deleteByExample")
    int deleteByExample(SingerExample example);

    @Delete({
        "delete from singer",
        "where sid = #{sid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer sid);

    @Insert({
        "insert into singer (sid, sname, ",
        "desc, pic)",
        "values (#{sid,jdbcType=INTEGER}, #{sname,jdbcType=INTEGER}, ",
        "#{desc,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR})"
    })
    int insert(Singer row);

    @InsertProvider(type=SingerSqlProvider.class, method="insertSelective")
    int insertSelective(Singer row);

    @SelectProvider(type=SingerSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="sid", property="sid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="sname", property="sname", jdbcType=JdbcType.INTEGER),
        @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
        @Result(column="pic", property="pic", jdbcType=JdbcType.VARCHAR)
    })
    List<Singer> selectByExample(SingerExample example);

    @Select({
        "select",
        "sid, sname, desc, pic",
        "from singer",
        "where sid = #{sid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="sid", property="sid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="sname", property="sname", jdbcType=JdbcType.INTEGER),
        @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
        @Result(column="pic", property="pic", jdbcType=JdbcType.VARCHAR)
    })
    Singer selectByPrimaryKey(Integer sid);

    @UpdateProvider(type=SingerSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("row") Singer row, @Param("example") SingerExample example);

    @UpdateProvider(type=SingerSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("row") Singer row, @Param("example") SingerExample example);

    @UpdateProvider(type=SingerSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Singer row);

    @Update({
        "update singer",
        "set sname = #{sname,jdbcType=INTEGER},",
          "desc = #{desc,jdbcType=VARCHAR},",
          "pic = #{pic,jdbcType=VARCHAR}",
        "where sid = #{sid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Singer row);
    /*
    * 查询歌手名字包含且人气最高的歌手
    * */
    @Select("select *,(SELECT count(uid) from user_follow_singer WHERE singer.sid = user_follow_singer.sid) fansNum from singer where sname like CONCAT('%', #{search}, '%') order by fansNum desc limit 0,1")
    @ResultMap("singerMap")
    Singer queryByName(String param);

    @Select("select * from singer where sid = #{sid}")
    @Results(id = "singerMap",value = {
            @Result(id = true,column = "sid",property = "sid"),
            @Result(column = "sname",property = "sname"),
            @Result(column = "desc",property = "desc"),
            @Result(column = "pic",property = "pic"),
            @Result(column = "sid",property = "albums",many = @Many(select = "com.music.mapper.AlbumMapper.findWithSongsBySid")),
            @Result(column = "sid",property = "singleSongs",many = @Many(select = "com.music.mapper.SingleSongMapper.findHotSongs")),
    })
    Singer findSingerWithAlbum(int sid);

    @Select("select * from singer where sid=#{sid}")
    Singer findBySid(int sid);

    @Select("select * from singer where sid in (select * from user_follow_singer where uid = #{uid})")
    List<Singer> findByUid(int uid);

    /*
    * 查询名字包含输入并按粉丝数倒序的歌手
    * */
    @Select("select *,(SELECT count(uid) from user_follow_singer WHERE singer.sid = user_follow_singer.sid) fansNum from singer where sname like CONCAT('%', #{search}, '%') order by fansNum desc")
    @ResultMap("singerMap")
    List<Singer> findSingerBySearch(String search);

    @Select("select count(*) from user_follow_singer where uid = #{uid} and sid=#{sid}")
    Integer isFollow(@Param("sid") Integer sid,@Param("uid") Integer uid);
}